class RLEIterator
{
public:
    RLEIterator(vector<int> &encoding)
    {
        this->encoding = encoding;
    }

    int next(int n)
    {
        int i = 0;
        while (i < n)
        {
            while (index < encoding.size() && encoding[index] == 0)
            {
                index += 2;
            }
            if (index >= encoding.size())
            {
                return -1;
            }
            if (n > encoding[index])
            {
                n -= encoding[index];
                encoding[index] = 0;
            }
            else
            {
                encoding[index] -= n;
                return encoding[index + 1];
            }
        }
        return -1;
    }

private:
    vector<int> encoding;
    int index = 0;
};

/**
 * Your RLEIterator object will be instantiated and called as such:
 * RLEIterator* obj = new RLEIterator(encoding);
 * int param_1 = obj->next(n);
 */